/*
 * Created on 2006-10-25
 *
 */
package edu.pku.sei.modeler.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.dialogs.MessageDialog;

import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaNode;

/**
 * @author zhangle
 *
 */
public class CreateSubNodeCommand extends CreateNodeCommand {
	private boolean isSubList;
	private String methodName;
	
	public CreateSubNodeCommand(MetaNode newObject, MetaDiagram parent, Rectangle bounds) {
		super(newObject, parent, bounds);
	}
	
	public void execute() {
		if(alreadyOnDiagram(node, diagram)) {
			MessageDialog.openWarning(null, "warning", "The element:"+node.getModelElement()+
					" is already on the diagram!");
			return;
		}
		node.setLocationX(bounds.getLocation().x);
		node.setLocationY(bounds.getLocation().y);
		node.setWidth(bounds.width);
		node.setHeight(bounds.height);
		node.setDiagram(diagram);
//		node.setModel(diagram.getModel());
		diagram.getNodes().add(node);
		
		if(parent != null) {
			parent.addToManager(node);
		}
		if(node.getModelElement() != null) {
//			MetaModel model = (MetaModel)diagram.getModel();
			MetaModelElement ele = node.getModelElement();
//			ele.setModel(model);
			// the model element contained by a reference shape
			// should NOT be added to the package
			if(node.isReferenceShape())return;
			// add the model element to its parent
			MetaModelElement parentEle = parent.getModelElement();
			if(parentEle != null && methodName != null && !"".equals(methodName)) {
				if(isSubList) {
					parentEle.metaAdd(ele, methodName);
				}
				else {
					parentEle.metaSet(ele, methodName);
				}
			}
		}
	}

	/**
	 * @param isSubList the isSubList to set
	 */
	public void setSubList(boolean isSubList) {
		this.isSubList = isSubList;
	}

	/**
	 * @param methodName the methodName to set
	 */
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
}
